﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBUtility;
using System.Data;

namespace DAL
{
    public class DbBackUpToAccess
    {
        public void BackUp(string path)
        {
            DCallRecords dCallRecords = new DCallRecords();
            DTaxiOnlineTime dTaxiOnlineTime = new DTaxiOnlineTime();
            DCompany dCompany = new DCompany();
            DComplaint dComplaint = new DComplaint();
            DDriver dDriver = new DDriver();
            DPhoneuser dPhoneuser = new DPhoneuser();
            DPrivateCar dPrivateCar = new DPrivateCar();
            DTaxi dTaxi = new DTaxi();
            DTaxiNoPsgDistanceConfig dTaxiNoPsg = new DTaxiNoPsgDistanceConfig();
            DTaxiRouteHistory dTaxiRouteHistory = new DTaxiRouteHistory();
            DTrade dTrade = new DTrade();
            DTaxiWarming dTaxiWarming = new DTaxiWarming();

            DbHelperAccess dbHelper = new DbHelperAccess(path);
            StringBuilder strWhere = new StringBuilder();
            strWhere.Append("'");
            strWhere.Append(DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"));
            strWhere.Append(" 00:00:00' and '");
            strWhere.Append(DateTime.Now.ToString("yyyy-MM-dd"));
            strWhere.Append(" 00:00:00");
            strWhere.Append("'");

            #region 平台来电去电记录
            DataSet ds = dCallRecords.GetList("calltime between "+strWhere);
            if (ds!=null && ds.Tables.Count>0)
            {
                         
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow row = ds.Tables[0].Rows[i];
                    string phoneNumber = row["phoneNumber"].ToString();                    
                    string type = row["type"].ToString();
                    string calltime = row["calltime"].ToString();

                    StringBuilder strSql = new StringBuilder();
                    strSql.Append("insert into tcallrecords(");
                    strSql.Append("phoneNumber,calltime,type)");
                    strSql.Append(" values ('");                    
                    strSql.Append(phoneNumber);
                    strSql.Append("','");
                    strSql.Append(calltime);
                    strSql.Append("','");
                    strSql.Append(type);
                    strSql.Append("')");                                      
                    bool result = dbHelper.ExecuteSQLNonquery(strSql.ToString());                
                    
                }
            }
            #endregion

            #region 车辆在线时间

            string datepreview = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
            DataSet dsOnlinetime = dTaxiOnlineTime.GetListBetween(datepreview, datepreview);
            if (dsOnlinetime != null && dsOnlinetime.Tables.Count > 0)
            {
                foreach (DataRow row in dsOnlinetime.Tables[0].Rows)
                {
                    string phone = row["PhoneNumber"].ToString();
                    string date = row["Date"].ToString();
                    string minutes = row["Minutes"].ToString();

                    StringBuilder strSql = new StringBuilder();
                    strSql.Append("insert into tcaronlinetime(");
                    strSql.Append("PhoneNumber,[Date],[Minutes])");
                    strSql.Append(" values ('");
                    strSql.Append(phone);
                    strSql.Append("','");
                    strSql.Append(date);
                    strSql.Append("','");
                    strSql.Append(minutes);
                    strSql.Append("')");
                    bool result = dbHelper.ExecuteSQLNonquery(strSql.ToString());    

                }
            }

            #endregion

            #region 公司列表
            DataSet dsCompany = dCompany.GetList("");
            if (dsCompany != null && dsCompany.Tables.Count > 0)
            {
                foreach (DataRow row in dsCompany.Tables[0].Rows)
                {
                    string id = row["Id"].ToString();
                    string name = row["CompanyName"].ToString();
                    string remark = row["Remark"].ToString();

                    StringBuilder strSql = new StringBuilder();
                    strSql.Append("insert into tcompany(");
                    strSql.Append("Id,CompanyName,Remark)");
                    strSql.Append(" values (");
                    strSql.Append(id);
                    strSql.Append(",'");
                    strSql.Append(name);
                    strSql.Append("','");
                    strSql.Append(remark);
                    strSql.Append("')");
                    bool result = dbHelper.ExecuteSQLNonquery(strSql.ToString());    
                }
            }
            #endregion

            #region 投诉记录

            DataSet dsComplaint = dComplaint.GetList("time between "+strWhere);
            if (dsComplaint != null && dsComplaint.Tables.Count > 0)
            {
                foreach (DataRow row in dsComplaint.Tables[0].Rows)
                {
                    string cobject = row["Complaintsobject"].ToString();
                    string creason = row["Complaintreason"].ToString();
                    string aid = row["ComplaintAid"].ToString();
                    string bid = row["ComplaintBid"].ToString();
                    string time = row["time"].ToString();                    

                    StringBuilder strSql = new StringBuilder();
                    strSql.Append("insert into tcomplaint(");
                    strSql.Append("Complaintsobject,Complaintreason");
                    strSql.Append(",ComplaintAid,ComplaintBid,[time])");
                    strSql.Append(" values (");
                    strSql.Append(cobject);
                    strSql.Append(",'");
                    strSql.Append(creason);
                    strSql.Append("','");
                    strSql.Append(aid);
                    strSql.Append("','");
                    strSql.Append(bid);
                    strSql.Append("','");
                    strSql.Append(time);
                    strSql.Append("')");
                    bool result = dbHelper.ExecuteSQLNonquery(strSql.ToString());
                }
            }

            #endregion

            #region 司机信息列表
            DataSet dsDriver = dDriver.GetList("");
            if (dsDriver != null && dsDriver.Tables.Count > 0)
            {
                foreach (DataRow row in dsDriver.Tables[0].Rows)
                {
                    string id = row["Id"].ToString();
                    string name = row["Name"].ToString();
                    string gender = row["Gender"].ToString();
                    string phone = row["PhoneNumber"].ToString();
                    string workId = row["WorkId"].ToString();
                    string taxiID = row["TaxiID"].ToString();
                    string remark = row["Remark"].ToString();
                    string imei = row["IMEI"].ToString();


                    StringBuilder strSql = new StringBuilder();
                    strSql.Append("insert into tdriver(");
                    strSql.Append("Id,Name,Gender,PhoneNumber,");
                    strSql.Append("WorkId,TaxiID,Remark,IMEI)");                    
                    strSql.Append(" values (");
                    strSql.Append(id);
                    strSql.Append(",'");
                    strSql.Append(name);
                    strSql.Append("','");
                    strSql.Append(gender);
                    strSql.Append("','");
                    strSql.Append(phone);
                    strSql.Append("','");
                    strSql.Append(workId);
                    strSql.Append("','");
                    strSql.Append(taxiID);
                    strSql.Append("','");
                    strSql.Append(remark);
                    strSql.Append("','");
                    strSql.Append(imei);
                    strSql.Append("')");
                    bool result = dbHelper.ExecuteSQLNonquery(strSql.ToString());
                }
            }
            #endregion

            #region 手机用户信息列表
            DataSet dsPhoneUser = dPhoneuser.GetListWhere("");
            if (dsPhoneUser != null && dsPhoneUser.Tables.Count > 0)
            {
                foreach (DataRow row in dsPhoneUser.Tables[0].Rows)
                {
                    string id = row["id"].ToString();
                    string phone = row["PhoneNumber"].ToString();
                    string loginCount = row["LoginCount"].ToString();
                    string lastLogin = row["LastLoginTime"].ToString();
                    string recommender = row["Recommender"].ToString();
                    string type = row["type"].ToString();
                    string firstLogin = row["FirstLoginTime"].ToString();
                    string phoneimei = row["PhoneIMEI"].ToString();
                    string phoneVersion = row["PhoneVersion"].ToString();

                    StringBuilder strSql = new StringBuilder();
                    strSql.Append("insert into tphoneuser(");
                    strSql.Append("id,PhoneNumber,LoginCount,LastLoginTime,");
                    strSql.Append("Recommender,type,FirstLoginTime,PhoneIMEI,");
                    strSql.Append("PhoneVersion)");
                    strSql.Append(" values (");
                    strSql.Append(id);
                    strSql.Append(",'");
                    strSql.Append(phone);
                    strSql.Append("','");
                    strSql.Append(loginCount);
                    strSql.Append("','");
                    strSql.Append(lastLogin);
                    strSql.Append("','");
                    strSql.Append(recommender);
                    strSql.Append("','");
                    strSql.Append(type);
                    strSql.Append("','");
                    strSql.Append(firstLogin);
                    strSql.Append("','");
                    strSql.Append(phoneimei);
                    strSql.Append("','");
                    strSql.Append(phoneVersion);
                    strSql.Append("')");
                    bool result = dbHelper.ExecuteSQLNonquery(strSql.ToString());
                }
            }
            #endregion

            #region 顺风车用户信息列表
            DataSet dsPrivateCar = dPrivateCar.GetListWhere("");
            if (dsPrivateCar != null && dsPrivateCar.Tables.Count > 0)
            {
                foreach (DataRow row in dsPrivateCar.Tables[0].Rows)
                {
                    string id = row["Id"].ToString();
                    string phone = row["PhoneNumber"].ToString();
                    string imei = row["IMEI"].ToString();
                    string registTime = row["RegistTime"].ToString();
                    string lastLogin = row["LastLoginTime"].ToString();
                    string signature = row["Signature"].ToString();
                    string taxiNo = row["TaxiNo"].ToString();
                    string taxiType = row["TaxiType"].ToString();
                    string name = row["DriverName"].ToString();
                    string gender = row["DriverGender"].ToString();

                    StringBuilder strSql = new StringBuilder();
                    strSql.Append("insert into tPrivateCar(");
                    strSql.Append("Id,PhoneNumber,IMEI,RegistTime,");
                    strSql.Append("LastLoginTime,Signature,TaxiNo,TaxiType,");
                    strSql.Append("DriverName,DriverGender)");
                    strSql.Append(" values (");
                    strSql.Append(id);
                    strSql.Append(",'");
                    strSql.Append(phone);
                    strSql.Append("','");
                    strSql.Append(imei);
                    strSql.Append("','");
                    strSql.Append(registTime);
                    strSql.Append("','");
                    strSql.Append(lastLogin);
                    strSql.Append("','");
                    strSql.Append(signature);
                    strSql.Append("','");
                    strSql.Append(taxiNo);
                    strSql.Append("','");
                    strSql.Append(taxiType);
                    strSql.Append("','");
                    strSql.Append(name);
                    strSql.Append("','");
                    strSql.Append(gender);
                    strSql.Append("')");
                    bool result = dbHelper.ExecuteSQLNonquery(strSql.ToString());
                }
            }
            #endregion

            #region 出租车信息列表
            DataSet dstaxi = dTaxi.GetList("");
            if (dstaxi != null && dstaxi.Tables.Count > 0)
            {
                foreach (DataRow row in dstaxi.Tables[0].Rows)
                {
                    string id = row["id"].ToString();
                    string taxiNo = row["TaxiNo"].ToString();
                    string taxiType = row["TaxiType"].ToString();
                    string remark = row["Remark"].ToString();
                    string taxiGroupid = row["TaxiGroupid"].ToString();
                    string serverId = row["ServerId"].ToString();
                    string companyId = row["CompanyId"].ToString();
                   
                    StringBuilder strSql = new StringBuilder();
                    strSql.Append("insert into ttaxi(");
                    strSql.Append("id,TaxiNo,TaxiType,Remark,");
                    strSql.Append("TaxiGroupid,ServerId,CompanyId)");
                    strSql.Append(" values (");
                    strSql.Append(id);
                    strSql.Append(",'");
                    strSql.Append(taxiNo);
                    strSql.Append("','");
                    strSql.Append(taxiType);
                    strSql.Append("','");
                    strSql.Append(remark);
                    strSql.Append("','");
                    strSql.Append(taxiGroupid);
                    strSql.Append("','");
                    strSql.Append(serverId);
                    strSql.Append("','");
                    strSql.Append(companyId);                 
                    strSql.Append("')");
                    bool result = dbHelper.ExecuteSQLNonquery(strSql.ToString());
                }
            }
            #endregion

            #region 出租车最大空载里程配置
            DataSet dsNopsgConfig = dTaxiNoPsg.GetList("");
            if (dsNopsgConfig != null && dsNopsgConfig.Tables.Count > 0)
            {
                foreach (DataRow row in dsNopsgConfig.Tables[0].Rows)
                {
                    string id = row["id"].ToString();
                    string phone = row["PhoneNumber"].ToString();
                    string starttime1 = row["StartTime1"].ToString();
                    string endtime1 = row["EndTime1"].ToString();
                    string distance1 = row["Distance1"].ToString();
                    string starttime2 = row["StartTime2"].ToString();
                    string endtime2 = row["EndTime2"].ToString();
                    string distance2 = row["Distance2"].ToString();

                    StringBuilder strSql = new StringBuilder();
                    strSql.Append("insert into ttaxinopsgdistanceconfig(");
                    strSql.Append("id,PhoneNumber,StartTime1,EndTime1,");
                    strSql.Append("Distance1,StartTime2,EndTime2,Distance2)");
                    strSql.Append(" values (");
                    strSql.Append(id);
                    strSql.Append(",'");
                    strSql.Append(phone);
                    strSql.Append("','");
                    strSql.Append(starttime1);
                    strSql.Append("','");
                    strSql.Append(endtime1);
                    strSql.Append("','");
                    strSql.Append(distance1);
                    strSql.Append("','");
                    strSql.Append(starttime2);
                    strSql.Append("','");
                    strSql.Append(endtime2);
                    strSql.Append("','");
                    strSql.Append(distance2);
                    strSql.Append("')");
                    bool result = dbHelper.ExecuteSQLNonquery(strSql.ToString());
                }
            }
            #endregion

            #region 出租车里程记录
            DataSet dsRouteHistory = dTaxiRouteHistory.GetList(
                "StartTime between"+strWhere);
            if (dsRouteHistory != null && dsRouteHistory.Tables.Count > 0)
            {
                foreach (DataRow row in dsRouteHistory.Tables[0].Rows)
                {
                   
                    string phone = row["PhoneNumber"].ToString();
                    string starttime = row["StartTime"].ToString();
                    string endtime = row["EndTime"].ToString();
                    string distance = row["Distance"].ToString();
                    string state = row["State"].ToString();
                    string serverTime = row["ServerTime"].ToString();
                
                    StringBuilder strSql = new StringBuilder();
                    strSql.Append("insert into ttaxiroutehistory(");
                    strSql.Append("PhoneNumber,StartTime,EndTime,");
                    strSql.Append("Distance,State,ServerTime)");
                    strSql.Append(" values ('");                    
                    strSql.Append(phone);
                    strSql.Append("','");
                    strSql.Append(starttime);
                    strSql.Append("','");
                    strSql.Append(endtime);
                    strSql.Append("','");
                    strSql.Append(distance);
                    strSql.Append("','");
                    strSql.Append(state);
                    strSql.Append("','");
                    strSql.Append(serverTime);               
                    strSql.Append("')");
                    bool result = dbHelper.ExecuteSQLNonquery(strSql.ToString());
                }
            }
            #endregion

            #region 交易记录
            DataSet dsTride = dTrade.GetList(
                "tradetime between" + strWhere);
            if (dsTride != null && dsTride.Tables.Count > 0)
            {
                foreach (DataRow row in dsTride.Tables[0].Rows)
                {

                    string type = row["Type"].ToString();
                    string phone = row["phoneNumber"].ToString();
                    string tradetime = row["tradetime"].ToString();
                    string phone2 = row["phoneNumber2"].ToString();
            
                    StringBuilder strSql = new StringBuilder();
                    strSql.Append("insert into ttrade(");
                    strSql.Append("Type,phoneNumber,tradetime,");
                    strSql.Append("phoneNumber2)");
                    strSql.Append(" values ('");
                    strSql.Append(type);
                    strSql.Append("','");
                    strSql.Append(phone);
                    strSql.Append("','");
                    strSql.Append(tradetime);
                    strSql.Append("','");
                    strSql.Append(phone2);                
                    strSql.Append("')");
                    bool result = dbHelper.ExecuteSQLNonquery(strSql.ToString());
                }
            }
            #endregion

            #region 出租车报警记录
            DataSet dsTaxiWarming = dTaxiWarming.GetList(
            "WarmingTime between" + strWhere);
            if (dsTaxiWarming != null && dsTaxiWarming.Tables.Count > 0)
            {
                foreach (DataRow row in dsTaxiWarming.Tables[0].Rows)
                {

                   
                    string phone = row["PhoneNumber"].ToString();
                    string state = row["Warmingstate"].ToString();
                    string time = row["WarmingTime"].ToString();

                    StringBuilder strSql = new StringBuilder();
                    strSql.Append("insert into twarming(");
                    strSql.Append("PhoneNumber,Warmingstate,WarmingTime)");                    
                    strSql.Append(" values ('");
                    strSql.Append(phone);
                    strSql.Append("','");
                    strSql.Append(state);
                    strSql.Append("','");
                    strSql.Append(time);                
                    strSql.Append("')");
                    bool result = dbHelper.ExecuteSQLNonquery(strSql.ToString());
                }
            }
            #endregion

            dbHelper.Close();
        }
    }
}
